package com.aptemo.webetek.server.data;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.aptemo.webetek.client.enums.EnumPickStatus;
import com.aptemo.webetek.shared.EventBean;
import com.aptemo.webetek.shared.PickBean;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Child of Bet
 * 
 * @author coh
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Pick
{
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key				id;

	private String			sid;

	@SuppressWarnings("unused")
	@Persistent
	@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
	private Key				parentKey;
	// @Persistent
	// private Bet bet;

	@Persistent
	private String			title;

	@Persistent
	private EnumPickStatus	status	= EnumPickStatus.NONE;

	@Persistent
	private int				picked	= 0;

	// prepared for bookies
	@Persistent
	private Double			odds	= Double.POSITIVE_INFINITY;

	@Persistent
	private Double			amount	= 0.0;

	public Pick(String title)
	{
		setTitle(title);
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getTitle()
	{
		return title;
	}

	public void setId(Key id)
	{
		this.id = id;
	}

	public Key getId()
	{
		return id;
	}

	public String getSid()
	{
		if (sid == null)
		{
			// auto create WEB SAFE key representation...
			this.sid = KeyFactory.keyToString(id);
		}

		return sid;
	}

	public void setPicked(int picked)
	{
		this.picked = picked;
	}

	public int getPicked()
	{
		return picked;
	}

	public void incrementPicked()
	{
		picked++;
	}

	public void decrementPicked()
	{
		if (picked > 0)
			picked--;
	}

	public PickBean toBean(EventBean bet)
	{
		PickBean bean = new PickBean(title);
		bean.setId(getSid());
		bean.setOdds(getOdds());
		bean.setStatus(status);
		bean.setPickedCnt(picked);
		bean.setEvent(bet);
		return bean;
	}

	public void setOdds(double odds)
	{
		this.odds = odds;
	}

	public Double getOdds()
	{
		// if (odds == null || odds.isNaN())
		// {
		// return 1.0;
		// }
		return odds;
	}

	// public Bet getBet()
	// {
	// return bet;
	// }

	public EnumPickStatus getStatus()
	{
		return status;
	}

	public void setStatus(EnumPickStatus status)
	{
		this.status = status;
	}

	public Bet getParent(PersistenceManager pm)
	{
		return pm.getObjectById(Bet.class, id.getParent());
	}

	public void incrementWeight(double amount)
	{
		// init...
		if (this.amount == null)
		{
			this.amount = 0.0;
		}
		this.amount += amount;
	}

	public Double getWeight()
	{
		if (this.amount == null)
		{
			// fallback / back compatible
			return picked + 0.0;
		}
		return this.amount;
	}

}
